-
Notifications
You must be signed in to change notification settings - Fork 626
fix(firestore): Further improved performance of UTF-8 string comparison logic #7098
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(firestore): Further improved performance of UTF-8 string comparison logic #7098
Conversation
Test Results 186 files + 144 186 suites +144 4m 41s ⏱️ + 3m 19s Results for commit 3c9caad. ± Comparison against base commit aad3da8. This pull request removes 325 and adds 1235 tests. Note that renamed tests count towards both.
♻️ This comment has been updated with latest results. |
Size Report 1Affected Products
Test Logs |
Startup Time Report 1Note: Layout is sometimes suboptimal due to limited formatting support on GitHub. Please check this report on GCS. Notes
Startup Times
|
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR replaces the previous UTF-8 byte-oriented string comparison with a more efficient surrogate-aware char-by-char algorithm and updates the changelog to document the fix.
- Simplified
compareUtf8Strings
to a single-pass char comparison usingCharacter.isSurrogate
- Removed
ByteString
fallback and eliminated codePoint/substring overhead - Added an unreleased entry in
CHANGELOG.md
for the performance fix
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
File | Description |
---|---|
firebase-firestore/src/main/java/com/google/firebase/firestore/util/Util.java | Rewrote compareUtf8Strings with optimized surrogate-aware logic; removed helper and ByteString paths |
firebase-firestore/CHANGELOG.md | Documented the UTF-8 comparison performance improvement under Unreleased |
Comments suppressed due to low confidence (2)
firebase-firestore/src/main/java/com/google/firebase/firestore/util/Util.java:90
- Update this Javadoc to describe the new surrogate-aware, char-by-char comparison algorithm rather than the previous byte-level UTF-8 logic.
/** Compare strings in UTF-8 encoded byte order */
firebase-firestore/src/main/java/com/google/firebase/firestore/util/Util.java:113
- Add unit tests covering edge cases around surrogate pairs versus non-surrogate characters to verify the new comparison logic across diverse Unicode inputs.
for (int i = 0; i < length; i++) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a performance improvement to the UTF-8 string comparison logic by avoiding object allocations. Suggestions have been provided to improve code readability.
firebase-firestore/src/main/java/com/google/firebase/firestore/util/Util.java
Outdated
Show resolved
Hide resolved
firebase-firestore/src/main/java/com/google/firebase/firestore/util/Util.java
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is wonderful, thanks Denver!
firebase-firestore/src/main/java/com/google/firebase/firestore/util/Util.java
Show resolved
Hide resolved
The semantics of this logic were originally fixed by #1967, but this fix caused a material performance degradation, which was then improved by #2021. The performance was, however, still suboptimal, and this PR further improves the speed back to close to its original speed and, serendipitously, simplifies the algorithm too. This commit effectively ports the following two PRs from the firebase-android-sdk repository: - firebase/firebase-android-sdk#7098 - firebase/firebase-android-sdk#7109
Further improved performance of UTF-8 string ordering logic, which had degraded in v25.1.2 due to the fix #6615 and received some improvements in v25.1.3 by the fix #6706. Even with these improvements, customers still observed a material performance degradation (#7053), which this PR addresses.
This PR was ported to the other Firestore SDKs:
Porting to firebase-ios-sdk is not required because it uses UTF-8 encoding natively. In contrast, Java and JavaScript use UTF-16 natively and require special comparison logic to order strings by their UTF-8 encoding lexicographically.
Fixes: #7053